package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.dao.BlogRoleDao;
import com.qinge.blog.dao.BlogUserRoleDao;
import com.qinge.blog.entity.*;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.qinge.blog.dao.BlogUserDao;
import com.qinge.blog.service.BlogUserService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogUser service层实现类
 */
@Service
public class BlogUserServiceImpl implements BlogUserService {

    @Resource
    BlogUserDao userDao;

    @Resource
    BlogUserRoleDao  userRoleDao;

    @Resource
    BlogRoleDao roleDao;



    @Override
    public void add(BlogUser... users) {
        addByList(Arrays.asList(users));
    }


    @Override
    public void addByList(List<BlogUser> users) {
        userDao.batchInsert(users);
    }



    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogUserExample example=new BlogUserExample();
        BlogUserExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        userDao.deleteByExample(example);
    }


    @Override
    public void removeById(String id) {
        userDao.deleteByPrimaryKey(id);
    }

    @Override
    public void update(BlogUser user) {
        userDao.upsert(user);
    }

    @Override
    public void upsert(BlogUser user) {
        userDao.upsert(user);
    }

    @Override
    public BlogUser findUserByUsername(String username) {
        BlogUserExample blogUserExample=new BlogUserExample();
        BlogUserExample.Criteria criteria=blogUserExample.createCriteria();

        if(username !=null){
            criteria.andUsernameEqualTo(username);
        }

        return userDao.selectByExample(blogUserExample).get(0);
    }


    @Override
    public List<String> findRolesByUserId(String userId) {
        BlogUserRoleExample userRoleExample =new BlogUserRoleExample();
        BlogUserRoleExample.Criteria criteria= userRoleExample.createCriteria();

        criteria.andUserIdEqualTo(userId);

        List<BlogUserRole> userRoles = userRoleDao.selectByExample(userRoleExample);

        List<String> roleIds = userRoles.stream().map(BlogUserRole::getRoleId).collect(Collectors.toList());

        BlogRoleExample roleExample=new BlogRoleExample();
        BlogRoleExample.Criteria roleCriteria=roleExample.createCriteria();

        if(roleIds.size()==0){
            return null;
        }

        roleCriteria.andIdIn(roleIds);

        List<BlogRole> roles= roleDao.selectByExample(roleExample);

        return roles.stream().map(BlogRole::getRoleName).collect(Collectors.toList());
    }


    @Override
    public BlogUser findById(String id) {
      return userDao.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogUser> findByName(String name) {
        BlogUserExample blogUserExample=new BlogUserExample();
        BlogUserExample.Criteria criteria=blogUserExample.createCriteria();

        if(name !=null){
            criteria.andUsernameLike(name);
            criteria.andNicknameLike(name);
        }

        return userDao.selectByExample(blogUserExample);
    }

    @Override
    public List<BlogUser> findByIds(List<String> ids) {
        BlogUserExample example=new BlogUserExample();
        BlogUserExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        return userDao.selectByExample(example);
    }

    @Override
    public PageInfo<BlogUser> findAll() {
        return paging(new PagingReqVO<BlogUser>());
    }

    @Override
    public PageInfo<BlogUser> findAll(PagingReqVO<BlogUser> pagingReqVO) {
       return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogUser> paging(PagingReqVO<BlogUser> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogUserExample blogUserExample=new BlogUserExample();
        BlogUserExample.Criteria criteria=blogUserExample.createCriteria();
        List<BlogUser> list=null;

        if(pagingReqVO.getParams()!=null){
            if (!StringUtils.isBlank(pagingReqVO.getParams().getUsername())){
                criteria.andUsernameLike('%'+pagingReqVO.getParams().getUsername()+'%');

                BlogUserExample.Criteria nickCriteria=blogUserExample.createCriteria();
                nickCriteria.andNicknameLike('%'+pagingReqVO.getParams().getUsername()+'%');

                blogUserExample.or(nickCriteria);
            }
        }

        list= userDao.selectByExample(blogUserExample);

        PageInfo<BlogUser> plist=new PageInfo<>(list);
        return plist;
    }


    @Override
    public void fillRoles(List<BlogUser> users) {
        for (BlogUser user:users) {
            user.setRoles(findRolesByUserId(user.getId()));
        }
    }

}

